package com.cyf.plc.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import com.cyf.plc.databinding.ItemAttributeBinding
import com.cyf.plc.model.PlcAttribute
import com.cyf.plc.viewholder.AttributeViewHolder

class AttributeAdapter(private val onItemClickListener: OnItemClickListener) :
    ListAdapter<PlcAttribute, AttributeViewHolder>(DIFF_CALLBACK) {

    interface OnItemClickListener {
        fun onItemClicked(plcAttribute: PlcAttribute, view: View)
    }

    companion object {
        private val DIFF_CALLBACK = object : DiffUtil.ItemCallback<PlcAttribute>() {

            override fun areItemsTheSame(
                oldItem: PlcAttribute,
                newItem: PlcAttribute
            ): Boolean =
                oldItem.attributeId == newItem.attributeId

            override fun areContentsTheSame(
                oldItem: PlcAttribute,
                newItem: PlcAttribute
            ): Boolean =
                oldItem == newItem
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) = AttributeViewHolder(
        ItemAttributeBinding.inflate(
            LayoutInflater.from(parent.context),
            parent,
            false
        )
    )

    override fun onBindViewHolder(holderSub: AttributeViewHolder, position: Int) {
        holderSub.bind(getItem(position), onItemClickListener)
    }
}